package com.schoolCommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolCommunity.entity.Information;
import com.schoolCommunity.entity.MailboxAdmin;
import com.schoolCommunity.mapper.MailboxAdminMapper;
import com.schoolCommunity.service.MailboxAdminService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author YTH
 * @since 2020-10-14
 */
@Service
public class MailboxAdminServiceImpl extends ServiceImpl<MailboxAdminMapper, MailboxAdmin> implements MailboxAdminService {

    @Resource
    public MailboxAdminMapper mailboxAdminMapper;

    //用户发送到管理员邮箱
    @Override
    public Information SendToAdmin(MailboxAdmin mailboxAdmin) {
        Information information = new Information();
        mailboxAdmin.setSendTime(new Date());
        int insert = mailboxAdminMapper.insert(mailboxAdmin);
        if (insert > 0) {
            information.setCode(200).setMessage("发送成功");
        } else {
            information.setCode(404).setMessage("发送失败");
        }
        return information;
    }

    @Override
    public Information GetSendFromMember(long memberId) {
        Information information = new Information();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("send_member_id", memberId);
        List list = mailboxAdminMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
            information.setCode(200).setMessage("邮箱获取成功").setData(list);
        } else {
            information.setCode(404).setMessage("没有邮箱数据");
        }
        return information;

    }

    @Override
    public Information GetNoReadMailToAdmin(long adminId) {
        Information information = new Information();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("get_admin_id", adminId);
        queryWrapper.eq("isGet", 0);
        List<MailboxAdmin> mailboxAdmins = mailboxAdminMapper.selectList(queryWrapper);
        if (mailboxAdmins.isEmpty()) {
            information.setCode(404).setMessage("当前没有需要处理的邮件");
        } else {
            information.setCode(200).setMessage("有尚未处理的邮件").setData(mailboxAdmins);
        }
        return information;

    }

    @Override
    public Information GetAllMail(long adminId) {
        Information information = new Information();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("get_admin_id", adminId);
        queryWrapper.eq("isGet",1);
        List<MailboxAdmin> mailboxAdmins = mailboxAdminMapper.selectList(queryWrapper);
        if (mailboxAdmins.isEmpty()) {
            information.setCode(404).setMessage("你没有收到的邮箱");
        } else {
            information.setCode(200).setMessage("获取所有邮箱成功").setData(mailboxAdmins);
        }
        return information;
    }

    @Override
    public Information queRenGetMail(long MailId) {
        Information information = new Information();
        MailboxAdmin mailboxAdmin = new MailboxAdmin();
        mailboxAdmin.setIsGet(1);
        mailboxAdmin.setId(MailId);
        mailboxAdmin.setGetTime(new Date());
        int i = mailboxAdminMapper.updateById(mailboxAdmin);
        if(i>0){
            information.setCode(200).setMessage("确认阅读");
        }else{
            information.setCode(404).setMessage("确认失败");
        }
        return information;
    }


}
